package qimao.com.edu.dao.impl;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import qimao.com.edu.dao.BaseDao;
import qimao.com.edu.dao.UserDao;
import qimao.com.edu.entity.Author;
import qimao.com.edu.entity.Users;

public class UserDaoImpl implements UserDao {

	// 获取所有用户
	@Override
	public List<Users> AllUsers() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Users> userList = new ArrayList<>();
		try {
			conn = BaseDao.getCon();
			String sql = "SELECT * FROM users";
			pstmt = conn.prepareStatement(sql);

			rs = pstmt.executeQuery();

			while (rs.next()) {
				Users user = new Users();
				user.setUserId(rs.getInt("user_id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setRoleName(rs.getString("rolename"));
				user.setNickname(rs.getString("nickname"));
				user.setAvatar(rs.getString("avatar"));

				Timestamp registerTimestamp = rs.getTimestamp("register_time");
		        if (registerTimestamp != null) {
		            user.setRegisterTime(registerTimestamp.toLocalDateTime());
		        }
				user.setStatus(rs.getInt("status"));

				userList.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(conn, pstmt, rs);
		}
		return userList;
	}

	// 用户注册
	@Override
	public int registerUser(Users user) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
//		avatar
		String sql = "INSERT INTO users(username,password,nickname,register_time,status) "
				+ "VALUES (?,?,?,NOW(),1)";

		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getNickname());
			result = ps.executeUpdate();
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;

	}

	// 根据ID获取用户
	@Override
	public Users userById(Integer user_id) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Users user = null;
		String sql = "SELECT * FROM users where user_id=?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setFloat(1, user_id);
			res = ps.executeQuery();
			while (res.next()) {
				user = new Users();
				user.setUserId(res.getInt("user_id"));
				user.setUsername(res.getString("username"));
				user.setPassword(res.getString("password"));
				user.setNickname(res.getString("nickname"));
				user.setAvatar(res.getString("avatar"));

				Date birthday = res.getDate("birthday");
				user.setBirthday(((java.sql.Date) birthday).toLocalDate());
				user.setRegisterTime(res.getTimestamp("register_time").toLocalDateTime());
				System.out.println(birthday);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}

		return user;
	}

	// 更新用户信息
	@Override
	public int updateUser(Users user) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "update users set username=?,password=?,status=? where user_id=?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ps.setInt(3, user.getStatus());
			ps.setFloat(4, user.getUserId());
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;

	}

	// 更新用户状态
	@Override
	public int updateUserStatus(Users user) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "update users set status=? where user_id=?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, user.getStatus());
			ps.setFloat(2, user.getUserId());
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	// 删除用户
	@Override
	public int deleteUser(Integer user_id) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "DELETE FROM users WHERE user_id = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setFloat(1, user_id);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	// 重置密码
	@Override
	public int resetPassword(Integer user_id, String password) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "UPDATE users set password = ? WHERE user_id = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, password);
			ps.setLong(2, user_id);
			result = ps.executeUpdate();
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;

	}

	@Override
	public Users selectUser(String username, String password) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Users user = null;
		String sql = "SELECT * FROM users WHERE username = ? and password = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				user = new Users();
				user.setUserId(res.getInt("user_id"));
				user.setUsername(res.getString("username"));
				user.setPassword(res.getString("password"));
				user.setNickname(res.getString("nickname"));
				user.setAvatar(res.getString("avatar"));

				Date birthday = res.getDate("birthday");
				if(birthday!=null) {
					user.setBirthday(((java.sql.Date) birthday).toLocalDate());
				}
				user.setRegisterTime(res.getTimestamp("register_time").toLocalDateTime());
				System.out.println(birthday);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return user;
	}

	@Override
	public Users selectUserByUsername(String username) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Users user = null;
		String sql = "SELECT * FROM users WHERE username = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, username);
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				user = new Users();
				user.setUserId(res.getInt("user_id"));
				user.setUsername(res.getString("username"));
				user.setPassword(res.getString("password"));
				user.setNickname(res.getString("nickname"));
				user.setAvatar(res.getString("avatar"));

				Date birthday = res.getDate("birthday");
				user.setBirthday(((java.sql.Date) birthday).toLocalDate());
				user.setRegisterTime(res.getTimestamp("register_time").toLocalDateTime());
				System.out.println(birthday);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return user;
	}


}
